package com.leps.nq.op.controller;

import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import com.leps.nq.op.entity.OpAlarm;
import com.leps.nq.op.service.IOpAlarmService;
import com.leps.nq.op.vo.AlarmQueryModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 报警日志表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/op/op-alarm")
@Api(tags = "报警信息管理")
public class OpAlarmController {

    @Autowired
    IOpAlarmService alarmService;

    @GetMapping
    @ApiOperation("报警信息列表")
    public R<PageVo<OpAlarm>> list(AlarmQueryModel queryModel) {
        PageVo<OpAlarm> pageVo = this.alarmService.list(queryModel);
        return RBuilder.ok(pageVo);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询报警信息")
    public R<OpAlarm> getById(@PathVariable Long id) {
        return RBuilder.ok(this.alarmService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增报警信息")
    @Log(title = "报警", businessType = BusinessType.INSERT)
    public R<?> save(@RequestBody OpAlarm alarm) {
        boolean save = this.alarmService.save(alarm);
        return RBuilder.ofBoolean(save);
    }

    @PutMapping
    @ApiOperation("修改报警信息")
    @Log(title = "报警", businessType = BusinessType.UPDATE)
    public R<?> update(@RequestBody OpAlarm alarm) {
        boolean b = this.alarmService.updateById(alarm);
        return RBuilder.ofBoolean(b);
    }

    @PutMapping("/handled")
    @ApiOperation("设置报警信息状态为已处理")
    @Log(title = "报警", businessType = BusinessType.OTHER)
    public R<?> handle(Long alarmId) {
        boolean update = this.alarmService.lambdaUpdate().set(OpAlarm::getHandled, 1L).eq(OpAlarm::getAlarmId, alarmId).update();
        return RBuilder.ofBoolean(update);
    }

    @DeleteMapping
    @ApiOperation("删除报警信息")
    @Log(title = "报警", businessType = BusinessType.DELETE)
    public R<?> remove(@RequestBody List<Long> alarmIds) {
        boolean b = this.alarmService.removeBatchByIds(alarmIds);
        return RBuilder.ofBoolean(b);
    }
}
